﻿using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Reflection;
using System.Globalization;

namespace Wicresoft.NRemedy.VS10Addin
{
    public class VSAddinUtil
    {
        private DTE2 _applicationObject;
        private AddIn _addInInstance;

        public VSAddinUtil(DTE2 applicationObject, AddIn addInInstance)
        {
            _applicationObject = applicationObject;
            _addInInstance = addInInstance;
        }

        private string GetCulturedMenuName(string englishName,int localId)
        {
            string result = englishName;

            try
            {
                string resourceName;
                ResourceManager resourceManager = new ResourceManager("Wicresoft.NRemedy.VS10Addin.Resource", Assembly.GetExecutingAssembly());
                CultureInfo cultureInfo = new CultureInfo(localId);

                if (cultureInfo.TwoLetterISOLanguageName == "zh")
                {
                    System.Globalization.CultureInfo parentCultureInfo = cultureInfo.Parent;
                    resourceName = String.Concat(parentCultureInfo.Name, englishName);
                }
                else
                {
                    resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, englishName);
                }

                result = resourceManager.GetString(resourceName);
            }
            catch
            {
                result = englishName;
            }

            return result;
        }

        private string GetCommandFullName(string cmdName)
        {
            return "NEnhancer.Connect." + cmdName;
        }

        public CommandBar GetCommandBarByName(string cmdBarName)
        {
            return ((CommandBars)_applicationObject.CommandBars)[cmdBarName];
        }

        public void AddNamedCommand2(
            CommandBar[] cmdBars, 
            string cmdName, 
            string buttonText, 
            string toolTip,
            bool useMsoButton, 
            int iconIndex, 
            int position)
        {
            // Do not try to add commands to a null bar
            if (cmdBars == null) { return; }

            // Get commands collection
            Commands2 commands = (Commands2)_applicationObject.Commands;
            object[] contextGUIDS = new object[] { };

            try
            {
                // Add command
                Command command = commands.AddNamedCommand2(_addInInstance, cmdName, buttonText, toolTip,
                    useMsoButton, iconIndex, ref contextGUIDS,
                    (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                    (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                if (command != null && cmdBars != null)
                {
                    foreach(CommandBar cb in cmdBars)
                        command.AddControl(cb, position);
                }
            }
            catch (ArgumentException ex)
            {
                // Command already exists, so ignore the exception.
                throw ex;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationObject">in</param>
        /// <param name="subItems">out</param>
        /// <param name="project">out</param>
        public static bool TryGetCurrentSubItemsAndContainerProj(
            DTE2 applicationObject,
            ref ProjectItems subItems,
            ref Project project,
            ref ProjectItem currentItem
            )
        {
            SelectedItems items = applicationObject.SelectedItems;
            if (items.Count != 1)
                return false;
            ProjectItem projItem = items.Item(1).ProjectItem;
            Project proj = items.Item(1).Project;
            //current is a folder or projItem
            if (projItem != null && proj == null)
            {
                currentItem = projItem;
                subItems = projItem.ProjectItems;
                project = projItem.ContainingProject;
            }
            //current is a project
            else if (projItem == null && proj != null)
            {
                currentItem = null;
                subItems = proj.ProjectItems;
                project = proj;
            }
            return true;
        }

        public static ProjectItem GetProjectItemByName(string targetItemName, ProjectItems container)
        {
            foreach (ProjectItem pi in container)
            {
                if (pi.Name == targetItemName)
                {
                    return pi;
                }
            }
            return null;
        }

        public static ProjectItem GetFirstProjectItemLikeName(string targetItemName, ProjectItems container)
        {
            foreach (ProjectItem pi in container)
            {
                if (pi.Name.ToLower().Contains(targetItemName))
                {
                    return pi;
                }
            }
            return null;
        }

    }
}
